package view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingConstants;

import model.CriminalRegModel;
import model.userModel;

import common.loadData;
import dao.Criminal_RegDao;
import dao.criminalDAO;
import dao.userdao;

public class panelCriminal extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JScrollPane ScrollPaneCriminal = null;
	private JTable TableCriminal = null;
	private JButton ButtonAdd = null;
	private JButton ButtonModify = null;
	private JButton ButtonDelete = null;
	public static int IdCriminal = -2;

	JFrame a;
	private JButton ButtonListCrimeType = null;
	private JButton ButtonListCrame = null;
	public panelCriminal(JFrame a) {
		super();
		this.a = a;
		initialize();
	}

	private void initialize() {
		this.setSize(735, 520);
		this.setLayout(null);
		this.add(getPanelTitle(), null);
		this.add(getScrollPaneCriminal(), null);
		this.add(getButtonAdd(), null);
		this.add(getButtonModify(), null);
		this.add(getButtonDelete(), null);
		this.add(getButtonListCrimeType(), null);
		this.add(getButtonListCrame(), null);
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setLocation(new Point(0, 0));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 24));
			lbtitle.setForeground(new Color(0, 153, 153));
			lbtitle.setText("List Criminals");
			lbtitle.setSize(new Dimension(176, 34));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.setLocation(new Point(269, 15));
			panelTitle.setSize(new Dimension(163, 33));
			panelTitle.add(lbtitle, null);
		}
		return panelTitle;
	}

	private JScrollPane getScrollPaneCriminal() {
		if (ScrollPaneCriminal == null) {
			ScrollPaneCriminal = new JScrollPane();
			ScrollPaneCriminal.setSize(new Dimension(646, 389));
			ScrollPaneCriminal.setLocation(new Point(44, 59));
			ScrollPaneCriminal.setViewportView(getTableCriminal());
		}
		return ScrollPaneCriminal;
	}

	private JTable getTableCriminal() {
			TableCriminal = new JTable(loadData.rowCriminal, loadData.CollunmCriminal);
			TableCriminal.setSize(new Dimension(643, 80));
			if(TableCriminal.getRowCount()==0)
			{
				JOptionPane.showMessageDialog(null, "No data for keyword '"+mainForm.search+"'!");
			}
		return TableCriminal;
	}

	private JButton getButtonAdd() {
		if (ButtonAdd == null) {
			ButtonAdd = new JButton();
			ButtonAdd.setText("Add");
			ButtonAdd.setLocation(new Point(353, 465));
			ButtonAdd.setSize(new Dimension(100, 33));
			ButtonAdd.setIcon(new ImageIcon(getClass().getResource("/image/Create.png")));
			ButtonAdd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					IdCriminal=-1;
					addCriminal add = new addCriminal();
					add.setVisible(true);
					a.dispose();
				}
			});
		}
		return ButtonAdd;
	}

	private JButton getButtonModify() {
		if (ButtonModify == null) {
			ButtonModify = new JButton();
			ButtonModify.setLocation(new Point(471, 466));
			ButtonModify.setText("Modify");
			ButtonModify.setIcon(new ImageIcon(getClass().getResource("/image/Modify.png")));
			ButtonModify.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonModify.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonModify.setSize(new Dimension(100, 33));
			ButtonModify.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if((TableCriminal.getSelectedRow())>=0)
					{
					int row = TableCriminal.getSelectedRow();
					int column = 0;
					IdCriminal = Integer.parseInt(TableCriminal.getValueAt(row, column).toString());
					addCriminal add = new addCriminal();
					add.setTitle("Update Infomation for Criminal");
					add.setVisible(true);
					a.dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Choice row!");
					}
				}
			});
		}
		return ButtonModify;
	}

	private JButton getButtonDelete() {
		if (ButtonDelete == null) {
			ButtonDelete = new JButton();
			ButtonDelete.setText("Delete");
			ButtonDelete.setSize(new Dimension(100, 33));
			ButtonDelete.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonDelete.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonDelete.setIcon(new ImageIcon(getClass().getResource("/image/Delete.png")));
			ButtonDelete.setLocation(new Point(589, 466));
			if(login.idrole==1)
			{
				ButtonDelete.setEnabled(false);
			}
			ButtonDelete.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int row = TableCriminal.getSelectedRow();
					int column = 0;
					IdCriminal = Integer.parseInt(TableCriminal.getValueAt(row, column).toString());
					CriminalRegModel model = new CriminalRegModel();
					model.setId(IdCriminal);
					int rsms = JOptionPane.showConfirmDialog(null, "Do you want to delete this Criminal?");
					if(rsms==0)
					{
						int rs=Criminal_RegDao.deleteCriminalReg(model);
						if(rs==1)
						{
							JOptionPane.showMessageDialog(null,"Delete User Criminal!");
							a.dispose();
							loadData.tab=2;
							(new mainForm()).setVisible(true);
						}
						else
						{
							JOptionPane.showMessageDialog(null,"Delete Criminal fail!");
						}
						
					}
				}
			});
		}
		return ButtonDelete;
	}

	private JButton getButtonListCrimeType() {
		if (ButtonListCrimeType == null) {
			ButtonListCrimeType = new JButton();
			ButtonListCrimeType.setLocation(new Point(45, 465));
			ButtonListCrimeType.setText("List Crime Type");
			ButtonListCrimeType.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonListCrimeType.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonListCrimeType.setIcon(new ImageIcon(getClass().getResource("/image/Listc.png")));
			ButtonListCrimeType.setSize(new Dimension(150, 33));
			ButtonListCrimeType.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					(new addCriminalType()).setVisible(true);
					
				}
			});
		}
		return ButtonListCrimeType;
	}

	private JButton getButtonListCrame() {
		if (ButtonListCrame == null) {
			ButtonListCrame = new JButton();
			ButtonListCrame.setLocation(new Point(211, 465));
			ButtonListCrame.setText("List Crame");
			ButtonListCrame.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonListCrame.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonListCrame.setIcon(new ImageIcon(getClass().getResource("/image/Text.png")));
			ButtonListCrame.setSize(new Dimension(126, 33));
			ButtonListCrame.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					(new addListCrame()).setVisible(true);
					
				}
			});
		}
		return ButtonListCrame;
	}

}
